define(["app","js/services/MuseService",'js/services/MessageBox'],
    function (app) {
        var deps = ["$scope", "$http", "$rootScope", "$filter", "$state", "$stateParams","MuseService","MessageBox"];

        function controller($scope, $http, $rootScope, $filter, $state, $stateParams,MuseService,MessageBox) {
            $scope.curUser = getSessionJson('curUserObj');

            if(isEmpty($scope.curUser)){
                var url = 'login.html';
                location.replace(url);
            }

            var id = $stateParams.id;

            $scope.car = {
                carnum:""

            };
            // 是否编辑
            $scope.isEdit = false;

            $scope.getCarDetail = function() {
                MuseService.post("car/car/"+id, {
                    id : id
                }).success(function(data) {
                    if (data.code == 0) {
                        $scope.car = data.data;

                        if(!data.data){
                            $scope.isEdit = false;
                            $scope.car = {
                                carnum:"",
                                cartype:"",
                                carzj:"",
                                carpl:""

                            };
                        }
                    } else {
                        MessageBox.error("查询失败:" + data.msg);
                    }
                });
            };
            // 有传参时才去查询
            if (id) {
                $scope.getCarDetail();
                $scope.isEdit = true;
            }

            // 保存发现 基本信息
            $scope.saveObj = function() {
                MuseService.postObj("car/saveCar/"+id , $scope.car).success(function(data) {
                    if (data.code == 0) {
                        MessageBox.notify("保存成功！");
                        history.back();
                    } else {
                        MessageBox.error("保存失败: " + data.msg);
                    }
                });
            };

            $scope.delCar = function(){
                MuseService.post("check/carpushdel", {id:$scope.car.id}).success(function(data) {
                    if (data.code == 0) {
                        MessageBox.notify("保存成功！");
                        history.back();
                    } else {
                        MessageBox.error("保存失败: " + data.msg);
                    }
                });
            }

            // 取消，返回
            $scope.close = function () {
                history.back();
            };
        }

        controller.$inject = deps;
        app.lazy.controller("CarDetailController", controller);
    });